package onion.molingo2o.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class DBHelperManager {

	public static final String CITY_LIST_DB = "adresslist.db";
	public static  int CITY_LIST_VER = 10;
	private DBHelper dbHelper;
	private SQLiteDatabase sqliteDB;

	public DBHelperManager(Context mContext){
		dbHelper = new DBHelper(mContext, CITY_LIST_DB, null, CITY_LIST_VER);
	}
	public HashMap<String, String[]> getProStringArray(){

		ArrayList<String> list1 = new ArrayList<String>();
		ArrayList<String> list2 = new ArrayList<String>();
		HashMap<String, String[]> map = new HashMap<String,String[]>();
		String[] str1 = null;
		String[] str2 = null;
		sqliteDB = dbHelper.getWritableDatabase();
		Cursor cursor = sqliteDB.query("province", new String[]{"proindex,proname"}, null, null, null, null, "_id");
		while(cursor.moveToNext()){

			String index = cursor.getString(cursor.getColumnIndex("proindex"));
			String name = cursor.getString(cursor.getColumnIndex("proname"));
			list1.add(name);
			list2.add(index);


		}
		str1 = new String[list1.size()];
		str2 = new String[list2.size()];
		for (int i = 0; i < list1.size(); i++) {

			str1[i] = list1.get(i); 
			str2[i] = list2.get(i); 
//			Log.i("DD",str1[i]);
//			Log.i("DD",str2[i]);
		}
		map.put("proindex", str2);
		map.put("proname",str1);
		closeDB(sqliteDB,cursor);
		return map;

	}

	public HashMap<String, String[]> getCityStringArray(String proIndex){

		ArrayList<String> list1 = new ArrayList<String>();
		ArrayList<String> list2 = new ArrayList<String>();
		HashMap<String, String[]> map = new HashMap<String,String[]>();
		String[] str1 = null;
		String[] str2 = null;
		sqliteDB = dbHelper.getWritableDatabase();
		Cursor cursor = sqliteDB.query("city", new String[]{"cityindex,cityname"}, "proindex = ?", new String[]{proIndex}, null, null, "_id");
		while(cursor.moveToNext()){

			String index = cursor.getString(cursor.getColumnIndex("cityindex"));
			String name  = cursor.getString(cursor.getColumnIndex("cityname"));
			list1.add(name);
			list2.add(index);

		}
		str1 = new String[list1.size()];
		str2 = new String[list2.size()];
		for (int i = 0; i < list1.size(); i++) {

			str1[i] = list1.get(i); 
			str2[i] = list2.get(i); 
//			Log.i("DD",str1[i]);
//			Log.i("DD",str2[i]);
		}
		map.put("cityindex", str2);
		map.put("cityname",str1);
		closeDB(sqliteDB,cursor);
		return map;

	}

	public HashMap<String, String[]> getTwonStringArray(String proIndex,String cityIndex){

		ArrayList<String> list1 = new ArrayList<String>();
		ArrayList<String> list2 = new ArrayList<String>();
		HashMap<String, String[]> map = new HashMap<String,String[]>();
		String[] str1 = null;
		String[] str2 = null;
		sqliteDB = dbHelper.getWritableDatabase();
		Cursor cursor = sqliteDB.query("town", new String[]{"townindex,townname"}, "proindex = ? and cityindex = ?", new String[]{proIndex,cityIndex}, null, null, "_id");
		while(cursor.moveToNext()){

			String index = cursor.getString(cursor.getColumnIndex("townindex"));
			String name = cursor.getString(cursor.getColumnIndex("townname"));
			list1.add(name);
			list2.add(index);


		}
		str1 = new String[list1.size()];
		str2 = new String[list2.size()];
		for (int i = 0; i < list1.size(); i++) {

			str1[i] = list1.get(i); 
			str2[i] = list2.get(i); 
		}
		map.put("townindex", str2);
		map.put("townname",str1);
		closeDB(sqliteDB,cursor);
		return map;

	}


	public String getAddressName(String proindex){
		sqliteDB = dbHelper.getWritableDatabase();
		Cursor cursor = sqliteDB.query("province", new String[]{"proname"}, "proindex = ?", new String[]{proindex}, null, null, "_id");
		if(cursor.moveToNext()){

			String name = cursor.getString(cursor.getColumnIndex("proname"));
			return name;
		}
		closeDB(sqliteDB,cursor);
		return "";
	}
	public String getAddressName(String proindex,String cityindex){
		sqliteDB = dbHelper.getWritableDatabase();
		Cursor cursor = sqliteDB.query("city", new String[]{"cityname"}, "proindex = ? and cityindex = ?", new String[]{proindex,cityindex}, null, null, "_id");
		if(cursor.moveToNext()){

			String name = cursor.getString(cursor.getColumnIndex("cityname"));
			return name;
		}

		if(cursor != null && !cursor.isClosed()){

			cursor.close();
			cursor = null;
			if(sqliteDB != null && sqliteDB.isOpen()){

				sqliteDB.close();
			}
		}
		closeDB(sqliteDB,cursor);
		return "";
	}
	public String getAddressName(String proindex,String cityindex,String townindex){
		sqliteDB = dbHelper.getWritableDatabase();
		Cursor cursor = sqliteDB.query("town", new String[]{"townname"}, "proindex = ? and cityindex = ? and townindex = ?", new String[]{proindex,cityindex,townindex}, null, null, "_id");
		if(cursor.moveToNext()){

			String name = cursor.getString(cursor.getColumnIndex("townname"));
			return name;
		}
		closeDB(sqliteDB,cursor);
		return "";
	}
   public String getAddressInfo(String proindex,String cityindex,String townindex){
   return getAddressName(proindex)+getAddressName(proindex,cityindex)+getAddressName(proindex,cityindex,townindex);
   }

	private void closeDB(SQLiteDatabase db,Cursor cursor){


		if(cursor != null && !cursor.isClosed()){

			cursor.close();
			cursor = null;
			if(db != null && db.isOpen()){

				db.close();
				db = null;
			}

		}else{

			if(db != null && db.isOpen()){

				db.close();
				db = null;
			}
		}
	}

}
